<?php 
class AdBrandAdmin
{
	/*
	public function ControllerAdCat()
	{
		self::runPreActions();
		self::runPostActions();
	}
	*/
	public function runPreActions()
	{
		global $commander;
		global $adc;
		switch($commander->getPostAction())
		{	
			case 'list':
				//if($_POST['new_item_title'] != '')
				//{ self::addAdBrand($_POST['new_item_title']); }
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'am_brands', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'am_brands', 0); }
				if($_POST['del_item_id'] > 0)
				{ self::deleteAdBrand($_POST['del_item_id']); }
				return;
			break;
			case 'add':
			{
				if($_POST['Title'] != '')
				{ self::addAdBrand($_POST['Title']); }
			}
			case 'del':
			{
				if($_POST['brandId'] > 0)
				{ self::deleteAdBrand($_POST['brandId']); }
			}
		}
	}
	
	public function runPostActions()
	{
		global $db;
		global $commander;
		global $adc;
		
		switch($commander->getPostAction())
		{
			case 'list':
				if((count($_POST['Id']) > 0) && ($adc->getAdBrands()))
				{
					$i = 0;
					foreach($_POST['Id'] as $id)
					{
						if(isset($adc->AdBrands[$id]))
						{
							if($_POST['Title'][$i] !== $adc->AdBrands[$id]->getTitle())
							{ 
								$adc->AdBrands[$id]->setTitle($commander->formatText($_POST['Title'][$i]));
								$adc->AdBrands[$id]->setDataChanged(true);
							}
							if($_POST['TypeId'][$i] != $adc->AdBrands[$id]->getTypeId())
							{ 
								$adc->AdBrands[$id]->setTypeId($_POST['TypeId'][$i]);
								$adc->AdBrands[$id]->setDataChanged(true);
							}
							if($_POST['CatId'][$i] != $adc->AdBrands[$id]->getCatId())
							{ 
								$adc->AdBrands[$id]->setCatId($_POST['CatId'][$i]);
								$adc->AdBrands[$id]->setDataChanged(true);
							}
							if($_POST['BrandOrder'][$i] != $adc->AdBrands[$id]->getBrandOrder())
							{ 
								$adc->AdBrands[$id]->setBrandOrder($_POST['BrandOrder'][$i]);
								$adc->AdBrands[$id]->setDataChanged(true);
							}
							if($_FILES['Image'.$id])
							{
								$file = $_FILES['Image'.$id];
								if($file["name"] != '')
								{
									if($file['size'] < 1000000)
									{ $adc->AdBrands[$id]->uploadImage($_FILES['Image'.$id]); }
								}
								$adc->AdBrands[$id]->setDataChanged(true);
							}
							$adc->AdBrands[$id]->update();
						}
						$i++;
					} //End foreach
					if($_POST['selected'])
					{
						foreach($_POST['selected'] as $id)
						{
							if(isset($adc->AdBrands[$id]))
							{
								if($_POST['activate'] == '-1')
								{
									$adc->AdBrands[$id]->setActive(1);
									$commander->setTableActive($id, 'am_brands', 1); 
								}
								if($_POST['deactivate'] == '-1')
								{
									$adc->AdBrands[$id]->setActive(0);
									$commander->setTableActive($id, 'am_brands', 0); 
								}
							}
						}
					}
				}
			break;
			//List-Editor group actions end
			
			//case 'add':
			//{
			//	if($_POST['Title'] != '')
			//	{ self::addAdBrand($_POST['Title']); }
			//}
			case 'del':
			{
				if($_POST['brandId'] > 0)
				{ self::deleteAdBrand($_POST['brandId']); }
			}
			
			//Editor AdBrand actions
			case 'editor_adbrand':
			case 'edit':
				if($adc->getCurrentAdBrandId() > 0)
				{
					if(is_object($adc->getCurrentAdBrand()))
					{
						if(stripslashes($_POST['Title']) !== $adc->CurrentAdBrand->getTitle())
						{ 
							$adc->CurrentAdBrand->setTitle($commander->formatText($_POST['Title'])); 
							$adc->CurrentAdBrand->setDataChanged(true);
						}
						if($_POST['TypeId'] != $adc->CurrentAdBrand->getTypeId())
						{ 
							$adc->CurrentAdBrand->setTypeId($_POST['TypeId']);
							$adc->CurrentAdBrand->setDataChanged(true);
						}
						if($_POST['CatId'] != $adc->CurrentAdBrand->getCatId())
						{ 
							$adc->CurrentAdBrand->setCatId($_POST['CatId']);
							$adc->CurrentAdBrand->setDataChanged(true);
						}
						if($_POST['BrandOrder'] != $adc->CurrentAdBrand->getBrandOrder())
						{ 
							$adc->CurrentAdBrand->setBrandOrder($_POST['BrandOrder']);
							$adc->CurrentAdBrand->setDataChanged(true);
						}
						if(stripslashes($_POST['Description']) !== $adc->CurrentAdBrand->getDescription())
						{ 
							$adc->CurrentAdBrand->setDescription($commander->formatText($_POST['Description']));
							$adc->CurrentAdBrand->setDataChanged(true);
						}
						if(intval($_POST['ImageDelete']) > 0)
						{ 
							$adc->CurrentAdBrand->deleteImage(); 
							$adc->CurrentAdBrand->setDataChanged(true);
						}
						$adc->CurrentAdBrand->update();
					}
				}
			break;
		}
	}
	
	function addAdBrand($title)
	{
		global $commander;
		$title = $commander->formatText($title);
		if($title != '')
		{
			$sql  = 'INSERT INTO '.DB_PERFIX.'am_brands (`title`)'; 
			$sql .= 'VALUES ("'.$title.'");';
			$result = mysql_query($sql);
		}
	}
	
	function deleteAdBrand($id)
	{	
		if($id > 0)
		{	
			$sql = 'UPDATE '.DB_PERFIX.'am_ads SET brandId=0 WHERE brandId='.intval($id).';';
			$result = mysql_query($sql);
			$sql = 'DELETE FROM '.DB_PERFIX.'am_brands WHERE id='.intval($id).' LIMIT 1;';
			$result = mysql_query($sql);
			return;
		}
	}
}
?>